package com.shisfish.library.Controller;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.jbarcode.JBarcode;
import org.jbarcode.encode.Code128Encoder;
import org.jbarcode.encode.InvalidAtributeException;
import org.jbarcode.paint.EAN8TextPainter;
import org.jbarcode.paint.WidthCodedPainter;

import com.shisfish.core.Test.ChangeImage;
import com.shisfish.core.Test.OneBarcodeUtil;
import com.shisfish.library.Impl.BarcodeImpl;
import com.shisfish.library.Model.Barcode;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("barcode")
public class BarcodeController extends GlobalVariable {

	private BarcodeImpl barcodeImpl = new BarcodeImpl();
	
	/*@GET
	@Path("test")
	@Produces("application/json; charset=utf-8")
	public void test() {
		
		int count = barcodeImpl.countAll();
		String code = 10000000 + count + 1 + "";
		System.out.println(code);
		JBarcode localJBarcode = new JBarcode(Code128Encoder.getInstance(),WidthCodedPainter.getInstance(),EAN8TextPainter.getInstance());  
		localJBarcode.setEncoder(Code128Encoder.getInstance()); 
        localJBarcode.setBarHeight(20);
		try {
			BufferedImage localBufferedImage = localJBarcode.createBarcode(code);
			OneBarcodeUtil.saveToJPEG(localBufferedImage, "EAN8.jpg");
			// 找到该张图片，
			String pic = ChangeImage.getImageString("EAN8.jpg");
			System.out.println(pic);
			
			Barcode barcode = new Barcode(code, pic);
			
			barcodeImpl.insertOne(barcode);
			
		} catch (InvalidAtributeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
	
	@GET
	@Path("createBarcode/{idadmin}/{number}")
	@Produces("application/json; charset=utf-8")
	public String createBarcode(@PathParam("idadmin") String idadmin, @PathParam("number") int number, 
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			if (number > 0) {
				int count = barcodeImpl.countAll();
				
				List<Barcode> barcodes = new ArrayList<Barcode>();
				
				for (int i = 0; i < number; i++) {
					String code = 10000000 + count + 1 + i + "";
					System.out.println(code);
					JBarcode localJBarcode = new JBarcode(Code128Encoder.getInstance(),WidthCodedPainter.getInstance(),EAN8TextPainter.getInstance());  
					localJBarcode.setEncoder(Code128Encoder.getInstance()); 
			        localJBarcode.setBarHeight(20);
					try {
						BufferedImage localBufferedImage = localJBarcode.createBarcode(code);
						OneBarcodeUtil.saveToJPEG(localBufferedImage, "EAN8.jpg");
						// 找到该张图片，
						String pic = ChangeImage.getImageString("EAN8.jpg");
						
						Barcode barcode = new Barcode(code, pic);
						
						barcodes.add(barcode);
						
					} catch (InvalidAtributeException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				int flag = barcodeImpl.insertAll(barcodes);
				
				if (flag == number) {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
				if (flag > 0 && flag < number) {
					globalMap.put("message", "部分未添加成功");
				}
				if (flag == 0) {
					globalMap.put(STATUS_CODE, INSERT_WR);
				}
				
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	/**
	 * @author shisfish
	 * @date 2016年3月11日 下午10:18:33
	 * @Description 查询所有未使用的条形码
	 * @param idadmin
	 * @param currentPage
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@GET
	@Path("selectNotUsed/{idadmin}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectNotUsed(@PathParam("idadmin") String idadmin, @PathParam("currentPage") int currentPage, 
			@PathParam("pageSize") int pageSize, @Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			int count = barcodeImpl.countNot();
			if (count > 0) {
				Page page = PageUtils.createPage(count, currentPage, pageSize);
				List<Barcode> barcodes = barcodeImpl.selectNotPage(page.getBeginIndex(), pageSize);
				if (barcodes.size() > 0) {
					globalMap.put(STATUS_CODE, SUCCESS);
					globalMap.put("page", page);
					globalMap.put("list", barcodes);
				} else {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectTypePage/{idadmin}/{type}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectTypePage(@PathParam("idadmin") String idadmin, @PathParam("currentPage") int currentPage, 
			@PathParam("pageSize") int pageSize, @Context HttpServletRequest request, 
			@PathParam("type") int type) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			int count = barcodeImpl.countType(type);
			if (count > 0) {
				Page page = PageUtils.createPage(count, currentPage, pageSize);
				List<Barcode> barcodes = barcodeImpl.selectTypePage(type, page.getBeginIndex(), pageSize);
				if (barcodes.size() > 0) {
					globalMap.put(STATUS_CODE, SUCCESS);
					globalMap.put("page", page);
					globalMap.put("list", barcodes);
				} else {
					globalMap.put(STATUS_CODE, SELECT_EMPTY);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
}
